Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

$(function(){
	//var date=new Date();
	//date.setMinutes(0,0,0);
//	$("#chartStartDate").val(date.Format("yyyy-MM-dd HH:mm:ss"));
//	$("#multiStationDate").val(date.Format("yyyy-MM-dd HH:mm:ss"));
	//date.setHours(date.getHours()-24);
//	$("#chartEndDate").val(date.Format("yyyy-MM-dd HH:mm:ss"));
	var height=document.body.clientHeight-70;
	
	$("#container").css("height",document.body.clientHeight-70+"px");
	openStationChart();
});
/**
 * 点击数据点请求统计图
 * @param {Object} feature
 * @param {Object} returnColumnArr
 */
function openStationChart(){
	var returnColumnArr="Station_Name,Datetime,Station_Id_C,PRS,TEM,RHU,PRE,WIN_D_S_Max,WIN_S_Max".split(",");
	var columnObj={};
//	var column0="";//站名
//	var column1="";//站号
//	var column2="";//经度
//	var column3="";//纬度
//	var column4="";//要素字段
	for(var i=0;i<returnColumnArr.length;i++){
		columnObj["column"+i]=returnColumnArr[i];
	}
	//var stationnum=station[columnObj["column1"]];
	var date=new Date();
	date.setMinutes(0,0,0);
	date.setHours(date.getHours()-8);
	var endDateStr=dateToStr(date);
	date.setHours(date.getHours()-12);
	var startDateStr=dateToStr(date);
	requestStationChartByStation(stationNum,startDateStr,endDateStr);
	
	
	
}
/**
 * 根据参数请求单站时次资料统计
 * @param {Object} stationnum
 * @param {Object} startDate
 * @param {Object} endDate
 */
function requestStationChartByStation(stationnum,startDate,endDate){
	var obj={};
	obj.stationNum=stationnum;
	obj.startDate=startDate;
	obj.endDate=endDate;
	var paramStr=JSON.stringify(obj);
	$.ajax({
			url:emspPath+"/PAMISS_oneMap/services/cimiss/requestDataByParam",
			type: "post",
			data:{"flag":"单站多时次统计图","paramJson":paramStr,"type":"other"},
			dataType: "json",
			scriptCharset:'utf-8',
			success:function(result){
				//alert(result);
				var jsonStr = result.data;
				var resData = eval('(' + jsonStr + ')');
				if(resData!=null){
					showOneStationChart(resData,stationnum,stationName,startDate,endDate);
				}
				//showPointDataByMeteo(id, resData, "micaps3", returnColumn,showTypeStr);
				//stationArr=resData;
			}
	});
}

function showOneStationChart(resData,stationnum,stationname,startDate,endDate){
	var returnColumnArr="Station_Name,Datetime,Station_Id_C,PRS,TEM,RHU,PRE,WIN_D_S_Max,WIN_S_Max".split(",");
	var columnObj={};
//	var column0="";//站名
//	var column1="";//时间 yyyy-MM-dd HH:mm:ss
//	var column2="";//站号
//	var column3="";//气压
//	var column4="";//气温
//	var column5="";//相对湿度
//	var column6="";//降水
//	var column7="";//最大风向
//	var column8="";//最大风速
	for(var i=0;i<returnColumnArr.length;i++){
		columnObj["column"+i]=returnColumnArr[i];
	}
	var startDateStr=startDate.substring(0,4)+"-"+startDate.substring(4,6)+"-"+startDate.substring(6,8)+
		" "+startDate.substring(8,10)+":"+startDate.substring(10,12)+":"+startDate.substring(12,14);
	var endDateStr=endDate.substring(0,4)+"-"+endDate.substring(4,6)+"-"+endDate.substring(6,8)+
		" "+endDate.substring(8,10)+":"+endDate.substring(10,12)+":"+endDate.substring(12,14);
	var sfDateStr=startDate.substring(4,6)+"-"+startDate.substring(6,8)+"-"+startDate.substring(0,4)+
		" "+startDate.substring(8,10)+":"+startDate.substring(10,12)+":"+startDate.substring(12,14);
	var efDateStr=endDate.substring(4,6)+"-"+endDate.substring(6,8)+"-"+endDate.substring(0,4)+
		" "+endDate.substring(8,10)+":"+endDate.substring(10,12)+":"+endDate.substring(12,14);
	var dateArr=[];//时间
	var tempArr=[];//气温
	var prsArr=[];//气压
	var preArr=[];//降水
	var rhuArr=[];//相对湿度
	var windArr=[];//风
	var tempNum=0;
	var sfDate=new Date(Date.parse(sfDateStr));
	var efDate=new Date(Date.parse(efDateStr));
	sfDate.setHours(sfDate.getHours()+8);
	efDate.setHours(efDate.getHours()+8);
	for(var i=Date.parse(sfDateStr);i<=Date.parse(efDateStr);i+=3600000){
		//console.info(new Date(i))
		var dateStr=new Date(i).Format("yyyy-MM-dd HH:mm:ss"); 
		var tempDate=new Date(i);
		tempDate.setHours(tempDate.getHours()+8);
		var insertDateStr=tempDate.Format("yyyy-MM-dd HH:mm:ss")
		dateArr.push(insertDateStr);
		if(resData.DS[tempNum][columnObj["column1"]]!=dateStr){
			tempArr.push(null);
			prsArr.push(null);
			preArr.push(null);
			rhuArr.push(null);
			windArr.push(null);
		}else{
			var resObj=resData.DS[tempNum];
			if(resObj.hasOwnProperty(columnObj["column4"])&&parseFloat(resObj[columnObj["column4"]])<10000){//气温
				tempArr.push(parseFloat(resObj[columnObj["column4"]]));
			}else{
				tempArr.push(null);
			}
			if(resObj.hasOwnProperty(columnObj["column3"])&&parseFloat(resObj[columnObj["column3"]])<10000){//气压
				prsArr.push(parseFloat(resObj[columnObj["column3"]]));
			}else{
				prsArr.push(null);
			}
			if(resObj.hasOwnProperty(columnObj["column6"])&&parseFloat(resObj[columnObj["column6"]])<10000){//降水
				preArr.push(parseFloat(resObj[columnObj["column6"]]));
			}else{
				preArr.push(null);
			}
			if(resObj.hasOwnProperty(columnObj["column5"])&&parseFloat(resObj[columnObj["column5"]])<10000){//相对湿度
				rhuArr.push(parseFloat(resObj[columnObj["column5"]]));
			}else{
				rhuArr.push(null);
			}
			if(resObj.hasOwnProperty(columnObj["column8"])&&parseFloat(resObj[columnObj["column8"]])<10000){//风
				var windObj={};
				windObj.y=parseFloat(resObj[columnObj["column8"]]);
				if(resObj.hasOwnProperty(columnObj["column7"])&&resObj[columnObj["column7"]]!=null){
					windObj.marker={};
					windObj.marker.symbol="url("+getWindImg(resObj[columnObj["column7"]])+")";
//					windObj.marker.symbol=null;
//					var tObj={symbol:getWindImg(resObj[columnObj["column7"]])};
//					windObj.marker=tObj;
				}
				windArr.push(windObj);
			}else{
				windArr.push(null);
			}
			tempNum++;
		}
		
	}
//	$("#chartForStationHD").html(stationname+"("+stationnum+")站多要素统计");
//	$("#chartStartDate").val(sfDate.Format("yyyy-MM-dd HH:mm:ss"));
//	$("#chartEndDate").val(efDate.Format("yyyy-MM-dd HH:mm:ss"));
	
	$('#container').highcharts({
        chart: {
            zoomType: 'xy'
        },
        title: {
            text: stationnum+stationName+'实况趋势图'
        },
        subtitle: {
            text: sfDate.Format("yyyy-MM-dd HH:mm:ss")+"至"+efDate.Format("yyyy-MM-dd HH:mm:ss")
        },
        xAxis: [{
            categories: dateArr,
            labels: {
                formatter: function() {
                    return this.value.toString().substring(11,13)+"时";
                }
            }
        }],
        yAxis: [{ // temp yAxis
            labels: {
                formatter: function() {
                    return this.value +'°C';
                },
                style: {
                    color: '#89A54E'
                }
            },
            title: {
                text: '温度',
                style: {
                    color: '#89A54E'
                }
            }

        }, { // rain yAxis
            gridLineWidth: 0,
            title: {
                text: '降水量',
                style: {
                    color: '#4572A7'
                }
            },
            labels: {
                formatter: function() {
                    return this.value +' mm';
                },
                style: {
                    color: '#4572A7'
                }
            }

        }, { // press yAxis
            gridLineWidth: 0,
            title: {
                text: '气压',
                style: {
                    color: '#AA4643'
                }
            },
            labels: {
                formatter: function() {
                    return this.value +' mb';
                },
                style: {
                    color: '#AA4643'
                }
            },
            opposite: true
        },{ // humidity yAxis
            labels: {
                formatter: function() {
                    return this.value +'%';
                },
                style: {
                    color: 'pink'
                }
            },
            title: {
                text: '湿度',
                style: {
                    color: 'pink'
                }
            },
            opposite: true

        },{ // wind yAxis
            labels: {
                formatter: function() {
                    return this.value +'m/s';
                },
                style: {
                    color: 'blue'
                }
            },
            title: {
                text: '风',
                style: {
                    color: 'blue'
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 100,
            verticalAlign: 'top',
            y: 0,
            floating: true,
            backgroundColor: '#FFFFFF'
        },
        series: [{
            name: '降水量',
            color: '#4572A7',
            type: 'column',
            yAxis: 1,
            visible: false,
            data: preArr,
            tooltip: {
                valueSuffix: ' mm'
            }

        }, {
            name: '气压',
            type: 'spline',
            color: '#AA4643',
            yAxis: 2,
            visible: false,
            data: prsArr,
            marker: {
                enabled: false
            },
            dashStyle: 'shortdot',
            tooltip: {
                valueSuffix: ' mb'
            }

        }, {
            name: '温度',
            color: '#89A54E',
            type: 'spline',
            yAxis: 0,
            data: tempArr,
            tooltip: {
                valueSuffix: ' °C'
            }
        },
        {
            name: '湿度',
            color: 'pink',
            type: 'spline',
            yAxis: 3,
            visible: false,
            data: rhuArr,
            tooltip: {
                valueSuffix: ' %'
            }
        },
        {
            name: '风',
            color: 'blue',
            type: 'spline',
            yAxis: 4,
            data: windArr,
            visible: false,
            tooltip: {
                valueSuffix: ' m/s'
            }
        }
        ]
    });
//	$("#chartForStation").show();
//	$("#chartForStation").css("z-index","77");
//	$("#oneStationChartStationSelect").val(stationnum+"");
//	$("#oneStationChartStationSelect").attr("value",stationnum+"");
//	$("#oneStationChartA").trigger("click");
}

/**
 * 根据风向获取风的图标
 * @param {Object} data
 * @return {TypeName} 
 */
function getWindImg(data){
	return "images/wind/"+Math.floor(data/22.5)+".png";
}
/**
 * 日期转字符串 yyyyMMddHHmmSS
 */
function dateToStr(date){
	var year=date.getFullYear()+"";
	var month="";
	if(date.getMonth()+1<10){
		month="0"+(date.getMonth()+1);
	}else{
		month=(date.getMonth()+1)+"";
	}
	var day="";
	if(date.getDate()<10){
		day="0"+date.getDate();
	}else{
		day=date.getDate()+"";
	}
	var hour="";
	if(date.getHours()<10){
		hour="0"+date.getHours();
	}else{
		hour=date.getHours()+"";
	}
	return year+""+month+""+day+""+hour+"0000";
}